package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.common.R;
import com.itheima.admin.mapper.CategoryMapper;
import com.itheima.admin.mapper.DishMapper;
import com.itheima.admin.mapper.FlavorsMapper;
import com.itheima.admin.pojo.*;
import com.itheima.service.DishService;
import com.itheima.util.PageUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/15 15:16
 * @PackageName:com.itheima.service.impl
 * @ClassName: DishServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorsMapper flavorsMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private Page ReturnPage;

    /**
     * 添加菜品
     *
     * @param dish
     * @return
     */
    @Override
    public R addDish(Dish dish) {
        List<Flavors> flavors = dish.getFlavors();
        int insert = dishMapper.insert(dish);
        System.out.println(insert);
        String id = dish.getId();

        //添加口味
        if (flavors!=null){
            for (Flavors flavor : flavors) {
                flavor.setDishId(id);
                flavor.setCreateTime(new Date());
                flavor.setCreateUser(dish.getCreateUser());
                flavorsMapper.insert(flavor);
            }
        }
        if (insert > 0) {
            return R.success("新增菜品成功");
        }
        return R.error("添加失败");
    }

    /**
     * 修改菜品
     *
     * @param dish
     * @return
     */
    @Override
    public R updateDish(Dish dish) {
        String id = dish.getId();
        QueryWrapper<Flavors> wrapper = new QueryWrapper<>();
        List<Flavors> flavors = dish.getFlavors();
        int update = dishMapper.updateById(dish);
//        删除对应餐品的口味信息
        wrapper.eq("dish_id", id);
        int delete = flavorsMapper.delete(wrapper);
        if (delete>0){
            System.out.println("菜品对应口味删除成功");
        }
//        添加新获取的口味信息
        if (flavors!=null){
            for (Flavors flavor : flavors) {
                flavor.setDishId(id);
                flavor.setUpdateTime(new Date());
                flavor.setUpdateUser(dish.getCreateUser());
                flavorsMapper.insert(flavor);
            }
        }
        if (update > 0) {
            return R.success("更新菜品成功");
        }
        return R.error("更新失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public R deleteDish(List<String> ids) {
        System.out.println("批量删除的id集合："+ids);
        int delete = dishMapper.deleteBatchIds(ids);
        int i = 0;
//        删除菜品下的口味信息
        for (String id : ids) {
            int delete1 = flavorsMapper.deleteflavors(id);
            if (delete1>0){
                i++;
            }
        }
        if (ids.size()==i){
            System.out.println("菜品对应口味删除成功");
        }
        if (delete > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    /**
     * 批量菜品停售/起售
     *
     * @param status
     * @param ids
     * @return
     */
    @Override
    public R updateDishStatus(int status, List<String> ids,String updateUser) {
        int i = 0;
        for (String id : ids) {
            UpdateWrapper<Dish> updateWrapper = new UpdateWrapper<>();

            updateWrapper.eq("id", id);

            updateWrapper.set("status", status);
            updateWrapper.set("update_time", new Date());
            updateWrapper.set("update_user", updateUser);
            dishMapper.update(null, updateWrapper);
            i++;
        }
        if (i == ids.size()) {
            return R.success("修改状态成功"+i);
        } else {
            return R.error("修改状态失败");
        }
    }

    /**
     * 下拉列表
     * @param
     * @return
     */
    @Override
    public R DropDownList(Dish dish) {
        /*添加条件*/
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dish.getCategoryId()!= null, Dish::getCategoryId, dish.getCategoryId());
        /*只查询起售的菜品*/
        wrapper.eq(Dish::getStatus, 1);
        /*按照sort排序，如果顺序相同就按照更新时间排序*/
        wrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishes = dishMapper.selectList(wrapper);

        int i =0;
        /*增加口味表和对应的菜品分类*/
        for (Dish dish1 : dishes) {

            List<Flavors> flavors = flavorsMapper.selectFlavors(dish1.getId());
            Category category = categoryMapper.selectById(dish1.getCategoryId());
            dish1.setCategoryName(category.getName());
            dish1.setFlavors(flavors);
            i++;
            System.out.println("添加分类名后对象的内容："+dish1);
            if (dishes.size()==i){
                System.out.println("第一："+dish1);
                System.out.println("第二："+dishes);
                return R.success(dishes);
            }
        }
        return R.success(dishes);
    }

        /**
     * 按照名字分页查询
     *
     * @param name
     * @param page
     * @param pageSize
     * @param type
     * @return
     */
    @Override
    public R showDishByName(String name, int page, int pageSize, int type) {
        int offset = PageUitl.getOffset(page, pageSize);

        int total = dishMapper.selectCountByName(Integer.valueOf(type),name);
        List<Dish> dish = dishMapper.selectByName(offset, pageSize,type,name);
        System.out.println("查询回来的内容:"+dish);
        ReturnPage.setTotal(total);
        ReturnPage.setCurrent(page);
        ReturnPage.setRecords(dish);
        ReturnPage.setSize(pageSize);
        System.out.println("准备返回的名字查询信息对象："+ReturnPage);
        if (dish!=null){
            return R.success(ReturnPage);
        }
        return R.error("查询异常");

    }

    /**
     * 菜品分页查询（全部）
     *
     * @param page
     * @param pageSize
     * @param type
     * @return
     */
    @Override
    public R showDish(int page, int pageSize, int type) {
        int offset = PageUitl.getOffset(page, pageSize);
        int total = dishMapper.selectCount(type);
        List<Dish> dish = dishMapper.selectAll(offset,pageSize,type);
        System.out.println("查询回来的内容:"+dish);
        ReturnPage.setTotal(total);
        ReturnPage.setCurrent(page);
        ReturnPage.setRecords(dish);
        ReturnPage.setSize(pageSize);
        System.out.println("准备返回的分页信息对象："+ReturnPage);
        if (dish!=null){
            return R.success(ReturnPage);
        }
        return R.error("查询异常");
    }

    /**
     * 按id查询
     *
     * @param id
     * @return
     */
    @Override
    public R selectDishById(String id) {
        QueryWrapper<Flavors> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id", id);
        List<Flavors> flavors = flavorsMapper.selectList(wrapper);
        if (flavors == null) {
            return R.error("查询口味时异常");
        }
        Dish dish = dishMapper.selectById(id);
        dish.setFlavors(flavors);
        System.out.println("按id查询结果：" + dish);
        if (dish != null) {
            return R.success(dish);
        }
        return R.error("按id查询失败");
    }
}
